package servlet;

import utils.MyDBUtil;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet(name = "UpdateServlet")
public class UpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sql = "UPDATE grade SET quiz=?,exam=? WHERE studentid=?";
        PreparedStatement ps = null;

        Connection conn = MyDBUtil.connetDB(); // get db connection
        int result = 0;
        try {

            ps = conn.prepareStatement(sql);
            ps.setInt(1, 100);
            ps.setInt(2, 100);
            ps.setString(3, "b63030007");

            result = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            MyDBUtil.closeDB();
        }

        String message = null;

        if(result == 0){
            message = "UPDATE FAIL";
        }else{
            message = "UPDATE SUCCESS";
        }

        request.setAttribute("message", message);
        RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
        rd.forward(request, response);
    }
}
